<?php

namespace app\api\controller;

use app\BaseController;
use think\facade\Db;

class Job extends BaseController
{
    /**
     * 添加队伍
     * @return [type] [description]
     */
    public function list()
    {
        $list = Db::name('job')->paginate(10)->toArray();

        foreach ($list['data'] as $key => $value) {
            $list['data'][$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
            $list['data'][$key]['typework_name'] = Db::name('jobclass')->where('id',$value['typework'])->value('name');
            $list['data'][$key]['work_name'] = Db::name('jobclass')->where('id',$value['typework'])->value('name');
        }

        if ($list) {
            return json(['code'=>1,'data'=>$list]);
        } else {
            return json(['code'=>0,'info'=>'获取失败']);
        }
    }

    /**
     * 发布职位
     */
    public function submit()
    {
        $data = $this->_vali([
            'name.require'      => '职位名称不能为空！',
            'typework.require'      => '所属工种不能为空！',
            'address.require'      => '地区不能为空！',
            'content.require'      => '工作清单不能为空！',
            'doorplate.require'      => '详细地址不能为空！',
            'phone.require'      => '联系电话不能为空！',
            'contacts.require'      => '联系人不能为空！',
            'longitude.require'      => '经度不能为空！',
            'latitude.require'      => '维度不能为空！',
            'start_time.require'      => '开始时间不能为空！',
            'end_time.require'      => '结束时间不能为空！',
            'city.require'      => '请选择地区',
            'drawing.require'      => '工程图纸不能为空',
        ],'POST');

        $data['uid'] = $this->request->users['id'];
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        $data['create_time'] = time();
        $data['update_time'] = time();

        $data['city'] = Db::name('area')->where('area_name',$data['city'])->value('area_id');

        $res = Db::name('job')->insert($data);

        if ($res) {
            return json(['code'=>1,'info'=>'添加成功']);
        } else {
            return json(['code'=>0,'info'=>'添加失败']);
        }
    }


    /**
     * 详情
     */
    public function get()
    {
        $input = $this->_vali([
            'id.require'          => '队伍名称不能为空！',
        ],'GET');

        $data = Db::name('job')->where('id',$input['id'])->find();

        if ($data) {
            $data['create_time'] = date("Y-m-d",$data['create_time']);
            $data['start_time'] = date("Y-m-d",$data['start_time']);
            $data['end_time'] = date("Y-m-d",$data['end_time']);
            $data['work_name'] = Db::name('jobclass')->whereIN('id',$data['typework'])->value('name');

            $arge = Db::name('area')->whereIN('area_id',$data['city'])->find();
            $parent = Db::name('area')->whereIN('area_no',$arge['parent_no'])->find();
            $data['city_name'] = $parent['area_name'].','.$arge['area_name'];

            return json(['code'=>1,'data'=>$data]);
        } else {
            return json(['code'=>0,'info'=>'获取失败']);
        }
    }
}